package com.sunny.ynhz.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.PatrolBean;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luao on 2017/6/7.
 */

public class PatrolDraftAdapter extends RecyclerView.Adapter<PatrolDraftAdapter.Myholder> implements View.OnClickListener {

    private List<PatrolBean> mItemLists;
    private DecimalFormat decimalFormat = new DecimalFormat("##0.00");

    @Override
    public void onClick(View view) {
        if (onBtnsClick != null) {
            switch (view.getId()) {
                case R.id.upload_patrol:
                    onBtnsClick.onUploadPatrolBtnsClick(mItemLists.get((int) view.getTag()), (int) view.getTag());
                    break;
                case R.id.delete_patrol:
                    onBtnsClick.onDeletePatrolBtnsClick(mItemLists.get((int) view.getTag()), (int) view.getTag());
                    break;
                default:
                    onBtnsClick.onItemClick(mItemLists.get((int) view.getTag()), (int) view.getTag());
            }
        }

    }

    public interface OnClickListener {
        void onItemClick(PatrolBean patrolBean, int position);

        void onUploadPatrolBtnsClick(PatrolBean patrolBean, int position);

        void onDeletePatrolBtnsClick(PatrolBean patrolBean, int position);
    }

    public void setOnBtnsClick(OnClickListener onBtnsClick) {
        this.onBtnsClick = onBtnsClick;
    }

    private OnClickListener onBtnsClick = null;


    public PatrolDraftAdapter(List<PatrolBean> mItemLists) {
        this.mItemLists = mItemLists;
    }


    @Override
    public Myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_patrol_draft, parent, false);
        Myholder myholder = new Myholder(view);
        return myholder;
    }

    @Override
    public void onBindViewHolder(Myholder holder, int position) {
        PatrolBean data = mItemLists.get(position);
        holder.startTime.setText(data.getStartTime());
        holder.endTime.setText(data.getEndTime());
        if (data.getDistance() != null) {
            holder.distance.setText("距离：" + decimalFormat.format(data.getDistance() / 1000) + "km");
        } else {
            holder.distance.setText("数据异常：无法获取巡河距离");
        }

        holder.itemView.setTag(position);
        holder.uploadPatrol.setTag(position);
        holder.deletePatrol.setTag(position);
        holder.itemView.setOnClickListener(this);
        holder.uploadPatrol.setOnClickListener(this);
        holder.deletePatrol.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mItemLists.size();
    }


    class Myholder extends RecyclerView.ViewHolder {
        @BindView(R.id.start_time)
        TextView startTime;
        @BindView(R.id.end_time)
        TextView endTime;
        @BindView(R.id.upload_patrol)
        TextView uploadPatrol;
        @BindView(R.id.delete_patrol)
        TextView deletePatrol;
        @BindView(R.id.distance)
        TextView distance;

        public Myholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }
}
